"use strict";

angular.module('app.granaryInfo').controller('GInfoCtrl', function($scope,HTTPDataService,Auths){

  if(!Auths.getUser())
    {
         window.location.href=baseurls+"#/login";
    } 
  
  
   //从视图中得到粮仓内的固定内容

  /* HTTPDataService.getGranaryInfo().then(function(response) {
           $scope.granaryDatas = response;
          
    });
*/

//配置分页基本参数
        $scope.paginationConf = {
            currentPage: 1,
            itemsPerPage: 10,
            pagesLength: 5,
            totalItems:20,
            perPageOptions: [10, 20, 30, 40, 50],
            onChange: function(){
            }
        };

        
     var GetAllEmployee = function () {
 
          HTTPDataService.getGranaryInfo($scope.paginationConf).then(function (response) {
               
                $scope.paginationConf.totalItems = response['sum'];
                $scope.granaryDatas = response['content'];

            });
 
        }
 
        $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetAllEmployee);
 

 
   HTTPDataService.getSystemConfig().then(function(response){
       
        $scope.systemDate = response;
    });

   //所有的粮食品种
      HTTPDataService.getGrainVariety().then(function(response){
          
          $scope.grainVariety = response; 
      });
   //所有的仓库管理员
      HTTPDataService.getGodownMan().then(function(response){
           
            $scope.godownMan = response;
      });

   //所有的检测员
      HTTPDataService.getDetectMan().then(function(response){
      
          $scope.detectMan = response;
     });
   $scope.editGranary = {
              
               granary_num:'',
               goods_location_num:'',
               grain_variety_id: '',
               grade:'',
               inventory_property:'',
               warehousing_moisture:'',
               producing_area:'',
               bulk_roughness:'',
               gluten_moisture:'',
               impurity:'',
               imperfect_grain:'',
               warehousing_time:'',
               detection_pepole:'',
               godown_man:'',
                id:''
             };
    $scope.showEditGranary = function(item){
         
      $scope.editGranary.id =  item.id;
      $scope.editGranary.granary_num =  item.granary_num;
      $scope.editGranary.goods_location_num =  item.goods_location_num;
      $scope.editGranary.grade =  item.grade;
      $scope.editGranary.grain_variety_id = item.grainVarietyId;
      $scope.editGranary.inventory_property = item.inventory_property;
      $scope.editGranary.warehousing_moisture = item.warehousing_moisture;
      $scope.editGranary.producing_area = item.producing_area;
      $scope.editGranary.bulk_roughness = item.bulk_roughness;
      $scope.editGranary.gluten_moisture = item.gluten_moisture;
      $scope.editGranary.impurity = item.impurity;
      $scope.editGranary.imperfect_grain = item.imperfect_grain;
      var val = Date.parse(item.warehousing_time);
      var newDate = new Date(val);
      $scope.editGranary.warehousing_time = newDate;
      
      $scope.editGranary.detection_pepole = item.detectionPeopleId;
      $scope.editGranary.godown_man = item.godownManId;
     $('#myModalEdit').modal('show') ;
     // alert(idvalue);
   };

   $scope.editGranarys = function(){
    $('#myModalEdit').modal('hide');                                             
    HTTPDataService.editGranaryInfoIn($scope.editGranary).then(function(response){
      
       if(response > 0)
       {
          HTTPDataService.getGranaryInfo($scope.paginationConf).then(function(response) {
               
                $scope.paginationConf.totalItems = response['sum'];
                $scope.granaryDatas = response['content'];

            });
 
      $.smallBox({
                    title: "编辑粮仓成功",
                    content: "<i class='fa fa-clock-o'></i> <i>编辑粮仓成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });
       }
       else
       {

       }     
    })
       
      
      
      
      } 
   

});